package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.connection.jdbconnection;

/**
 * Servlet implementation class CheckLogin
 */
public class CheckLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
    Connection con;
    Statement st;
    ResultSet rs;
    
   

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doPost(request, response);	
	}

	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		PrintWriter writer=response.getWriter();
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		String adminName=getServletContext().getInitParameter("adminName");
		String adminPassword=getServletContext().getInitParameter("password");
		HttpSession session=request.getSession();
		session.setAttribute("userName", userName);
		
		System.out.println(userName);
		System.out.println(password);
		System.out.println(adminName);
		System.out.println(adminPassword);
		
		if(adminName.equals(userName)   && adminPassword.equals(password))
		{
		session.setAttribute("adminName", adminName);
		session.setAttribute("password", adminPassword);
		response.sendRedirect("AdminHome.jsp");
		}
		else
		{
		boolean flag=false;
		String status=null;
		 try {
			 jdbconnection db=new jdbconnection();
				st=db.jdbConnection();
    	 }
    	 catch (Exception e) {
		e.printStackTrace();
		}
		
		String query="SELECT * FROM  register";// WHERE username='"+userName+"' AND password='"+password+"'";
		try {
			rs=st.executeQuery(query);
			while(rs.next())
			{
			
			if(userName.equals(rs.getString("userName")) && (password.equals(rs.getString("password"))))
			{
			  status=rs.getString("status");
			  System.out.println(status);
			  flag=true;
			  break;
			}
		
			}
			if(flag)
			{
				if(status.equals("1"))
				{
					session.setAttribute("userName", userName);
					session.setAttribute("password", password);
					response.sendRedirect("FacultyHome.jsp");
				}
				else 
				{
					session.setAttribute("unverified", new Object());
					response.sendRedirect("Login.jsp");
					
					writer.println("<h3> You are not verified yet. Try later.</h3>");
					writer.println("<a href=index.jsp> Back</a>");
				}
		
			}
			else {
				session.setAttribute("error", new Object());
				response.sendRedirect("Login.jsp");
				return;
				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		}// else 
		
		
	}

}
